﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.ComponentModel
Public Class Empresas
    Inherits BDDatos
    Sub New()
        Me.Tabla = "Empresas"
        Me.Vista = "VEmpresas"
    End Sub
    Public ID As String
    Public DV As String
    Public NOMBRE As String
    Public DIRECCION As String
    Public TELEFONO As String
    Public EMAIL As String
    Public TIP_DOC As String
    Public DPTO As String
    Public MUNiCIPIO As String
    Public CELULAR As String
    Public FAX As String
    Public REP_LEGAL As String
    Public CONTACTO As String
    Public IDPK As String
    Public CED_REP_LEGAL As String
    Public Function Insert() As String
        Me.Conectar()
        Me.ComenzarTransaccion()
        Try
            Me.querystring = "Insert into EMPRESAS" & _
            "(ID, TIPO_ID, DV, NOMBRE, REP_LEGAL, CONTACTO, DIRECCION, TELEFONO, EMAIL, CELULAR, FAX, DPTO, MUNICIPIO,CED_REP_LEGAL)" & _
            "Values" & _
            "(:ID, :TIP_ID, :DV, :NOMBRE, :REP_LEGAL, :CONTACTO, :DIRECCION, :TELEFONO, :EMAIL, :CELULAR, :FAX, :DPTO, :MUNICIPIO,:CED_REP_LEGAL)"
            Me.CrearComando(querystring)
            If Me.TIP_DOC = "NI" Then
                DV = Util.ValidarDigitoVerificacion(ID)
            End If
            Me.AsignarParametroCadena(":ID", Me.ID)
            Me.AsignarParametroCadena(":DV", Me.DV)
            Me.AsignarParametroCadena(":NOMBRE", UCase(Me.NOMBRE))
            Me.AsignarParametroCadena(":DIRECCION", UCase(Me.DIRECCION))
            Me.AsignarParametroCadena(":TELEFONO", Me.TELEFONO)
            Me.AsignarParametroCadena(":EMAIL", Me.EMAIL)
            Me.AsignarParametroCadena(":TIP_ID", Me.TIP_DOC)
            Me.AsignarParametroCadena(":DPTO", Me.DPTO)
            Me.AsignarParametroCadena(":MUNICIPIO", Me.MUNiCIPIO)
            Me.AsignarParametroCadena(":CELULAR", Me.CELULAR)
            Me.AsignarParametroCadena(":REP_LEGAL", Me.REP_LEGAL)
            Me.AsignarParametroCadena(":CONTACTO", Me.CONTACTO)
            Me.AsignarParametroCadena(":FAX", Me.FAX)
            Me.AsignarParametroCadena(":CED_REP_LEGAL", CED_REP_LEGAL)
            Me.num_reg = EjecutarComando()
            Me.Msg = "Se realizo la Operacion Exitosamente - " + num_reg.ToString + " Filas Afectadas"
            Me.lErrorG = False
            Me.ConfirmarTransaccion()
        Catch ex As Exception
            CancelarTransaccion()
            Me.lErrorG = True
            Me.Msg = "Err: " + ex.Message
        Finally
            Me.Desconectar()
        End Try
        Return Msg
    End Function
    Public Function Update() As String
        Me.Conectar()
        Me.ComenzarTransaccion()
        Try
            Me.querystring = "Update EMPRESAS SET " & _
            "ID=:ID, TIPO_ID=:TIP_ID, DV=:DV, NOMBRE=:NOMBRE, REP_LEGAL=:REP_LEGAL, CED_REP_LEGAL=:CED_REP_LEGAL, CONTACTO=:CONTACTO, DIRECCION=:DIRECCION, TELEFONO=:TELEFONO, EMAIL=:EMAIL, CELULAR=:CELULAR, FAX=:FAX, DPTO=:DPTO, MUNICIPIO=:MUNICIPIO WHERE ID=:IDPK"
            Me.CrearComando(querystring)
            If Me.TIP_DOC = "NI" Then
                DV = Util.ValidarDigitoVerificacion(ID)
            End If
            Me.AsignarParametroCadena(":ID", Me.ID)
            Me.AsignarParametroCadena(":DV", Me.DV)
            Me.AsignarParametroCadena(":NOMBRE", UCase(Me.NOMBRE))
            Me.AsignarParametroCadena(":DIRECCION", UCase(Me.DIRECCION))
            Me.AsignarParametroCadena(":TELEFONO", Me.TELEFONO)
            Me.AsignarParametroCadena(":EMAIL", Me.EMAIL)
            Me.AsignarParametroCadena(":TIP_ID", Me.TIP_DOC)
            Me.AsignarParametroCadena(":DPTO", Me.DPTO)
            Me.AsignarParametroCadena(":MUNICIPIO", Me.MUNiCIPIO)
            Me.AsignarParametroCadena(":CELULAR", Me.CELULAR)
            Me.AsignarParametroCadena(":REP_LEGAL", Me.REP_LEGAL)
            Me.AsignarParametroCadena(":CONTACTO", Me.CONTACTO)
            Me.AsignarParametroCadena(":FAX", Me.FAX)
            Me.AsignarParametroCadena(":CED_REP_LEGAL", CED_REP_LEGAL)
            Me.AsignarParametroCadena(":IDPK", Me.IDPK)
            Me.num_reg = EjecutarComando()
            Me.Msg = "Se realizo la Operacion Exitosamente - " + num_reg.ToString + " Filas Afectadas"
            Me.lErrorG = False
            Me.ConfirmarTransaccion()
        Catch ex As Exception
            CancelarTransaccion()
            Me.lErrorG = True
            Me.Msg = "Err: " + ex.Message
        Finally
            Me.Desconectar()
        End Try
        Return Msg
    End Function
    <DataObjectMethodAttribute(DataObjectMethodType.Select, True)> _
    Public Function GetPersonas(ByVal Buscar As String) As DataTable
        Me.Conectar()
        If Not String.IsNullOrEmpty(Buscar) Then
            Me.querystring = "Select * from VEMPRESAS where ID like :Buscar or upper(nombre) like :Buscar"
            CrearComando(querystring)
            Me.AsignarParametroCadena(":Buscar", UCase(Buscar))
            Me.AsignarParametroCadena(":Buscar", UCase(Buscar))
        Else
            Me.querystring = "Select * from VEMPRESAS"
            CrearComando(querystring)
        End If
        Dim dt As DataTable = EjecutarConsultaDataTable()
        Me.Desconectar()
        Return dt
    End Function
    <DataObjectMethodAttribute(DataObjectMethodType.Select, True)> _
    Public Function GetbyPK(ByVal Cedula As String) As DataTable
        Me.Conectar()
        Me.querystring = "Select * from VEMPRESAS where ID=:cedula"
        CrearComando(querystring)
        Me.AsignarParametroCadena(":cedula", Cedula)
        Dim dt As DataTable = EjecutarConsultaDataTable()
        Me.Desconectar()
        Return dt
    End Function
    <DataObjectMethodAttribute(DataObjectMethodType.Select, True)> _
    Public Function GetEmpresaCon() As DataTable
        Me.Conectar()
        Me.querystring = "Select * from VEMPRESAS where ID=:cedula"
        CrearComando(querystring)
        Me.AsignarParametroCadena(":cedula", Me.usuario)
        Dim dt As DataTable = EjecutarConsultaDataTable()
        Me.Desconectar()
        Return dt
    End Function
    <DataObjectMethodAttribute(DataObjectMethodType.Select, True)> _
    Public Function GetEmpresa() As DataTable
        Me.Conectar()
        Me.querystring = "Select * from VEmpresas where Id=:cedula"
        CrearComando(querystring)
        Me.AsignarParametroCadena(":cedula", Me.usuario)
        Dim dt As DataTable = EjecutarConsultaDataTable()
        Me.Desconectar()
        Return dt
    End Function
End Class
